<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>图片添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">

    <div class="layui-form-item">
        <label class="layui-form-label required">图片跳转链接</label>
        <div class="layui-input-block">
            <input type="text" name="srcimg" id="srcimg" lay-verify="required" lay-reqtext="图片跳转链接不能为空" placeholder="请输入图片链接"
                   value="" class="layui-input">
            <input type="text" name="addressimg" id="addressimg">
        </div>
    </div>
    <!--TODO 上传之后图标需要显示出来-->
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label required">图标</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="file">
                <i class="layui-icon">&#xe67c;</i>点击上传
            </button>
            <span id="spanimg"></span>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认提交</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>

<script>
    layui.use(['form'], function () {
        var icon = '';
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$,
            upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#file', //绑定元素
            url: '../slideshowimg/uploadIcon/', //上传接口
            size: 500,
            done: function(res){
                //上传完毕回调
                //图片上传地址
                icon = res.content
                console.log(icon);
                console.log(res);
                $("#addressimg").val(icon);
                $("#spanimg").text("上传成功");
            },
            error: function(){
                //请求异常回调
                console.log("网络错误！")
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            console.log(data.field)
            data.field.file = icon
            var spanimg=$("#spanimg").text();
            if(spanimg!="上传成功"){
                layer.msg("图片未上传~!");
                return false;
            }
            $.ajax({
                type: "post",
                url: "../slideshowimg/addhomepageimg",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(data.field),
                success: function (result) {
                    if (result.result == "success") {
                        layer.msg("提交成功~~~！");
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        //parent.$("#shousuobtn",parent.document).trigger('click');	//子页面局部刷新父页面事件
                        window.parent.document.getElementById("shousuobtn").click();
                    }else if (result.result == "error"){
                        layer.msg("提交失败~请重新在提交！！");
                    }
                },
                error: function () {
                    console.log("网络出错！请重新连接")
                }
            });

            return false;
        });

    });
</script>
</body>
</html>